package de.negativrekord.blockworld.geometry;

import cc.creativecomputing.graphics.CCDrawMode;
import cc.creativecomputing.graphics.CCVBOMesh;
import cc.creativecomputing.math.CCMath;

public class BlockCreator {
	
	private CCVBOMesh _myMesh;
	private float texW = 1700f;
	private float texH = 300f;
	
	
	int N_VERTICES;

	public BlockCreator (int nVertices) {
		N_VERTICES = nVertices;
		_myMesh = null;
		_myMesh = new CCVBOMesh(CCDrawMode.QUADS, N_VERTICES);
	}
	
	public CCVBOMesh mesh() {
		return _myMesh;
	}
	
	float scale = 200f;
	void addSideX (int x, int y, int z, int size, int height) {
		_myMesh.addVertex (x,y,z);
		_myMesh.addVertex (x+size,y,z);
		_myMesh.addVertex (x+size,y+height,z);
		_myMesh.addVertex (x,y+height,z);
		
		x+=100000;
		y+=100000;
		z+=100000;
		size = CCMath.abs(size);
		_myMesh.addTextureCoords (x/scale, y/scale);
		_myMesh.addTextureCoords ((x+size)/scale, y/scale);
		_myMesh.addTextureCoords ((x+size)/scale, (y+height)/scale);
		_myMesh.addTextureCoords (x/scale, (y+height)/scale);
		
//		_myMesh.addTextureCoords(1, 0f, 0f);
//		_myMesh.addTextureCoords(1, 1f, 0f);
//		_myMesh.addTextureCoords(1, 1f, 1f);
//		_myMesh.addTextureCoords(1, 0f, 1f);
	}
	
	void addSideY (int x, int y, int z, int size) {
		_myMesh.addVertex (x,y,z);
		_myMesh.addVertex (x,y,z+size);
		_myMesh.addVertex (x+size,y,z+size);
		_myMesh.addVertex (x+size,y,z);
	
		x+=100000;
		y+=100000;
		z+=100000;
		size = CCMath.abs(size);
		_myMesh.addTextureCoords (x/scale, z/scale);
		_myMesh.addTextureCoords (x/scale, (z+size)/scale);
		_myMesh.addTextureCoords ((x+size)/scale, (z+size)/scale);
		_myMesh.addTextureCoords ((x+size)/scale, z/scale);
		
//		_myMesh.addTextureCoords(1, 0f, 0f);
//		_myMesh.addTextureCoords(1, 1f, 0f);
//		_myMesh.addTextureCoords(1, 1f, 1f);
//		_myMesh.addTextureCoords(1, 0f, 1f);
	}
	
	void addSideZ (int x, int y, int z, int size, int height) {
		_myMesh.addVertex (x,y,z);
		_myMesh.addVertex (x,y,z+size);
		_myMesh.addVertex (x,y+height,z+size);
		_myMesh.addVertex (x,y+height,z);
		
		x+=100000;
		y+=100000;
		z+=100000;
		size = CCMath.abs(size);
		_myMesh.addTextureCoords (y/scale, z/scale);
		_myMesh.addTextureCoords (y/scale, (z+size)/scale);
		_myMesh.addTextureCoords ((y+height)/scale, (z+size)/scale);
		_myMesh.addTextureCoords ((y+height)/scale, z/scale);
		
//		_myMesh.addTextureCoords(1, 0f, 0f);
//		_myMesh.addTextureCoords(1, 1f, 0f);
//		_myMesh.addTextureCoords(1, 1f, 1f);
//		_myMesh.addTextureCoords(1, 0f, 1f);
	}
	
	public void addBox (int x, int y, int z, int size, int height) {
		addSideX (x-size/2, y-height/2, z-size/2, size, height);
		addSideX (x-size/2, y-height/2, z+size/2, size, height);  
		
		addSideY (x-size/2, y-height/2, z-size/2, size);
		addSideY (x-size/2, y+height/2, z-size/2, size);  
//		
		addSideZ (x-size/2, y-height/2, z-size/2, size, height);
		addSideZ (x+size/2, y-height/2, z-size/2, size, height);  
	}
}
